﻿using System;
using System.Windows.Forms;
using PersonalAccoutingProgram.Data;
using System.Linq;

public partial class References : Form
{ 
    private string userName;

    public References(string username)
    {
        InitializeComponent();
        this.userName = username;        
        InitializeControls();
    }

    private void InitializeControls()
    {
        InitializeDatePickers();
        InitializeExpensesType();
        InitializeIncomesType();
        InitializeBalanceType();
        InitializeExpensesCategories();
        InitializeIncomesCategories();
    }

    private void InitializeDatePickers()
    {
        dateTimePicker.Format = DateTimePickerFormat.Custom;
        dateTimePicker.CustomFormat = DateFormat.DATE_TIME_FORMAT;
        dateTimePicker1.Format = DateTimePickerFormat.Custom;
        dateTimePicker1.CustomFormat = DateFormat.DATE_TIME_FORMAT;
        dateTimePicker2.Format = DateTimePickerFormat.Custom;
        dateTimePicker2.CustomFormat = DateFormat.DATE_TIME_FORMAT;
    }

    private void InitializeBalanceType()
    { 
        comboBlanceType.Items.Add(ReferenceTypes.AVG_DAY);
        comboBlanceType.Items.Add(ReferenceTypes.TOTAL_DAY);
        comboBlanceType.Items.Add(ReferenceTypes.TOTAL_МОNTH);
        comboBlanceType.Items.Add(ReferenceTypes.TOTAL_YEAR);     
       
        comboBlanceType.SelectedIndex = 0;      
    }

    private void InitializeIncomesType()
    { 
        comboIncomesType.Items.Add(ReferenceTypes.OVERALL);
        comboIncomesType.Items.Add(ReferenceTypes.AVG_DAY);
        comboIncomesType.Items.Add(ReferenceTypes.TOTAL_DAY);
        comboIncomesType.Items.Add(ReferenceTypes.TOTAL_МОNTH);
        comboIncomesType.Items.Add(ReferenceTypes.TOTAL_YEAR);

        comboIncomesType.SelectedIndex = 0;    
    }

    private void InitializeIncomesCategories()
    {
        OriginDataClassesDataContext originDC =
        new OriginDataClassesDataContext();
        var allCategories =
        from currentCategory in originDC.GetTable<Origin>()
        select currentCategory.OriginName;
        comboIncomesCategory.Items.Add(String.Empty);
        comboIncomesCategory.Items.AddRange(allCategories.ToArray());
    }

    private void InitializeExpensesType()
    {
        comboType.Items.Add(ReferenceTypes.OVERALL);
        comboType.Items.Add(ReferenceTypes.AVG_DAY);
        comboType.Items.Add(ReferenceTypes.TOTAL_DAY);
        comboType.Items.Add(ReferenceTypes.TOTAL_МОNTH);
        comboType.Items.Add(ReferenceTypes.TOTAL_YEAR);
  
        comboType.SelectedIndex = 0;
    }

    private void InitializeExpensesCategories()
    {
        CategoryDataClassesDataContext referencesDC = 
        new CategoryDataClassesDataContext();
        var allCategories =
        from currentCategory in referencesDC.GetTable<Category>()
        select currentCategory.CategoryName;
        comboCategory.Items.Add(String.Empty);
        comboCategory.Items.AddRange(allCategories.ToArray());
    }

    private System.Drawing.Printing.PrintDocument docToPrint =
    new System.Drawing.Printing.PrintDocument();

    private void btnPrint_Click(object sender, EventArgs e)
    {
        printDialog.AllowSomePages = true;
        printDialog.ShowHelp = true;
        printDialog.Document = docToPrint;
        DialogResult result = printDialog.ShowDialog();
        if (result == DialogResult.OK)
        {
            docToPrint.Print();
        }
    }

    private void document_PrintPage(object sender,
                                    System.Drawing.Printing.PrintPageEventArgs e)
    {
        string text = rtbReference.Text;
        System.Drawing.Font printFont = new System.Drawing.Font
        ("Arial", 35, System.Drawing.FontStyle.Regular);
        e.Graphics.DrawString(text, printFont,
                              System.Drawing.Brushes.Black, 10, 10);
    }

    private void btnIncomesReference_Click(object sender, EventArgs e)
    { 
        string category = comboIncomesCategory.Text.Trim();
        int userChoice = comboIncomesType.SelectedIndex;
        DateTime date = DateTime.Parse(dateTimePicker.Text);

        switch (userChoice)
        {
            case 0:
                comboCategory.Text = String.Empty;                
                rtbReference.Text = 
                IncomesReferenceMaker.GetAllAmountsForCurrentUser(
                    userName, category);
                break;
            case 1:
                decimal incomesAverage = IncomesReferenceMaker.MakeAverageReferenceDay(
                    date  , userName, category);
                rtbReference.Text = 
                    String.Format("Average incomes for {0} {1} are {2:C}.",
                                  dateTimePicker.Text, category, incomesAverage);
                break;
            case 2:
                decimal incomesDayTotal = IncomesReferenceMaker.MakeTotalReferenceDay(
                    date, userName, category);
                rtbReference.Text = 
                    String.Format("Total incomes for {0} {1} are {2:C}.",
                                  dateTimePicker.Text, category, incomesDayTotal);
                break;                
            case 3:
                decimal incomesMothTotal = IncomesReferenceMaker.MakeTotalReferenceMonth(
                    dateTimePicker.Text, userName, category);
                DateTime currentDateMonth = DateTime.Parse(dateTimePicker.Text);
                rtbReference.Text =
                    String.Format("Total incomes for {0} {1} are {2:C}.",
                                  currentDateMonth.ToString("Y"), category, incomesMothTotal);
                break;               
            case 4:
                 decimal incomesYearTotal = IncomesReferenceMaker.MakeTotalReferenceYear(
                    dateTimePicker.Text, userName, category);
                DateTime currentDateYear = DateTime.Parse(dateTimePicker.Text);
                rtbReference.Text = 
                    String.Format("Total incomes for {0} {1} are {2:C}.",
                                  currentDateYear.Year, category, incomesYearTotal);
                break;
        }
    }

    private void btnMakeReference_Click_1(object sender, EventArgs e)
    {
        DateTime date = DateTime.Parse(dateTimePicker.Text);       
        int userChoice = comboType.SelectedIndex;
        string category = comboCategory.Text.Trim();

        switch (userChoice)
        {
            case 0:                            
                rtbReference.Text = 
                ExpensesReferenceMaker.GetAllAmountsForCurrentUser(userName, category);
                break;
            case 1:
                decimal expensesAverage =
                ExpensesReferenceMaker.MakeAverageReferenceDay(
                    date, userName, category);
                rtbReference.Text = 
                String.Format("Average expenses for {0} {1} are {2:C}.",
                              dateTimePicker.Text, category, expensesAverage);
                break;
            case 2:
                      decimal expensesDayTotal = 
                ExpensesReferenceMaker.MakeTotalReferenceDay(
                    date, userName, category);
                rtbReference.Text = 
                String.Format("Total expenses for {0} {1} are {2:C}.",
                              dateTimePicker.Text, category, expensesDayTotal);
                break;
               
            case 3:
                 decimal expensesMonthTotal =
                ExpensesReferenceMaker.MakeTotalReferenceMonth(
                    dateTimePicker.Text, userName, category);
                DateTime currentDateMonth = DateTime.Parse(dateTimePicker.Text);
                rtbReference.Text = 
                String.Format("Total expenses for {0} {1} are {2:C}.",
                              currentDateMonth.ToString("Y"), category, expensesMonthTotal);
                break;               
            case 4:
           decimal expensesYearTotal = 
                ExpensesReferenceMaker.MakeTotalReferenceYear(
                    dateTimePicker.Text, userName, category);
                DateTime currentDateYear = DateTime.Parse(dateTimePicker.Text);
                rtbReference.Text =
                String.Format("Total expenses for {0} {1} are {2:C}.",
                              currentDateYear.Year, category, expensesYearTotal);
                break;
        }
    }

    private void btnMakeBalanceReference_Click(object sender, EventArgs e)
    {
        string category = String.Empty; 
        int userChoice = comboBlanceType.SelectedIndex;
        DateTime date = DateTime.Parse(dateTimePicker.Text);

        switch (userChoice)
        {
            case 0:
                
                decimal expensesAverage = ExpensesReferenceMaker.MakeAverageReferenceDay(
                    date, userName, category);
                decimal incomesAverage = IncomesReferenceMaker.MakeAverageReferenceDay(
                    date, userName, category);
                decimal balanceAverage = incomesAverage - expensesAverage;
                rtbReference.Text = String.Format("Average balance for {0} {1} is {2:C}.",
                                                  dateTimePicker.Text, category, balanceAverage);
                break;
            case 1:
                decimal expensesDayTotal = ExpensesReferenceMaker.MakeTotalReferenceDay(
                    date, userName, category);
                decimal incomesDayTotal = IncomesReferenceMaker.MakeTotalReferenceDay(
                    date, userName, category);
                decimal balanceDayTotal = incomesDayTotal - expensesDayTotal;
                rtbReference.Text = 
                    String.Format("Total balance for {0} {1} is {2:C}.",
                                  dateTimePicker.Text, category, balanceDayTotal);
                break;
                
            case 2:
                DateTime currentDateMonth = DateTime.Parse(dateTimePicker.Text);
                decimal expensesMonthTotal = ExpensesReferenceMaker.MakeTotalReferenceMonth(
                    dateTimePicker.Text, userName, category);
                decimal incomesMonthTotal = IncomesReferenceMaker.MakeTotalReferenceMonth(
                    dateTimePicker.Text, userName, category);
                decimal balanceMonthTotal = incomesMonthTotal - expensesMonthTotal;
                
                rtbReference.Text = 
                    String.Format("Total balance for {0} {1} is {2:C}.",
                                 currentDateMonth.ToString("Y"), category, balanceMonthTotal);
                break;                
            case 3:
                decimal expensesYearTotal = ExpensesReferenceMaker.MakeTotalReferenceYear(
                    dateTimePicker.Text, userName, category);
                decimal incomesYearTotal = IncomesReferenceMaker.MakeTotalReferenceYear(
                    dateTimePicker.Text, userName, category);
                decimal balanceYearTotal = incomesYearTotal - expensesYearTotal;
                DateTime currentDateYear = DateTime.Parse(dateTimePicker.Text);
                rtbReference.Text = 
                    String.Format("Total balance for {0} {1} is {2:C}.",
                                  currentDateYear.Year, category, balanceYearTotal);
                break;
                
        }
    }

    private void btnSave_Click(object sender, EventArgs e)
    {
        saveFileDialog.DefaultExt = "*.rtf";
        saveFileDialog.Filter = "RTF Files|*.rtf";
        if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK
            && (saveFileDialog.FileName.Length > 0))
        {
            try
            {
                rtbReference.SaveFile(saveFileDialog.FileName);
            }
            catch
            {
                MessageBox.Show("Error saving file!",
                                "Error",
                                MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}